package org.secangkirkopipanas.gxtcore.server.util;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static Logger _logger = Logger.getLogger(HibernateUtil.class);
	public static final String HIBERNATE_KEY_FILE = "hibernate.ky";
	private static Configuration configuration;
	private static SessionFactory sessionFactory;

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			configuration = new AnnotationConfiguration().configure();
			// getCredentials(configuration);
			sessionFactory = configuration.buildSessionFactory();
			if (_logger.isDebugEnabled()) {
				_logger
						.debug("Create the SessionFactory from hibernate.cfg.xml");
			}
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			_logger.error("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	// private static void getCredentials(Configuration conf) throws IOException
	// {
	// InputStream in = HibernateUtil.class.getResourceAsStream("/"
	// + HIBERNATE_KEY_FILE);
	// String text = PasswordUtil.readCryptedFile(in);
	// in.close();
	// int idx1 = text.indexOf("\r\n");
	// String user = text.substring(0, idx1).trim();
	// String pwd = text.substring(idx1).trim();
	// conf.setProperty("hibernate.connection.username", user);
	// conf.setProperty("hibernate.connection.password", pwd);
	//	}
}
